<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use mikkle\tp_redis\Redis;
use think\Request;
use think\Cache;

class Post extends Backend
{
	
	public function postlist(){
        $redis = new Redis();
        $postlist = [];
        // 文章id
        $listkey = 'LIST:ARTICLE';
        // 帖子内容
        $hashkey = 'HASH:ARTICLE';
		// 从redis中读取帖子 存在
        if($redis->exists($listkey)){
            // 存放所有帖子id
            $lists = $redis->lRanges($listkey,0,-1);
            foreach ($lists as $k=>$v){
                $postlist[] = $redis->hVals($hashkey.$v);
            }
        }else{
            // 查询帖子表
            $postlist = model('Post.Post')->paginate(7,false,['query' => Request::instance()->param()]);

            // 将数据存入redis
//            foreach ($postlist as $k=>$v){
//                $obj = [
//                    'id' => $v['id'],
//                    'title' => $v['title'],
//                    'content' => $v['content'],
//                    'point' => $v['point'],
//                    'createtime' => $v['createtime'],
//                    'state' => $v['state'],
//                    'visit' => $v['visit'],
//                    'userid' => $v['userid'],
//                    'accept' => $v['accept'],
//                    'cateid' => $v['cateid'],
//                    'deletetime' => $v['deletetime'],
//                    'visit_count'=> $v['visit_count'],
//                ];
//                // 将帖子id存放在$listkey当中
//                $redis->rPush($listkey,$v['id']);
//                // 将帖子存放到$hashkey
//                $redis->hMset($hashkey.$v['id'],$obj);
//            }
        }
		
		return $this->fetch('',[
			
			'postlist' => $postlist
			
		]);
		
	}
   // 封装自定义函数，传入对象
    public function object_array($object)
    {

        if(is_object($object))
        {

            $array = (array)$object;

        }

        if(is_array($object))
        {
            foreach($object as $key=>$value)
            {

                $array[$key] =$this->object_array($value);

            }
        }

        return $array;

    }
	// 查看帖子
	public function details(){
		
		if($this->request->isAjax()){
			
			// 获取数据
			$id = $this->request->param('id');
				
			$result = model('Post.Post')->where(['id' => $id])->find();
			
			if($result === FALSE)
			{
				$this->error('查询失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('查询成功',url('admin/post/postlist'),$result,0);
			}
			
		}
		
	}
	
	// 删除帖子
	public function delpost(){
		
		if($this->request->isAjax()){
			
			// 获取数据
			$arrstr = $this->request->param('str');
			
			// 字符串转数组
			$arr = explode(',',$arrstr);
			
			// 循环
			foreach($arr as $item){
				
				$result = model('Post.Post')->destroy(['id' => $item]);
				
			}
			
			if($result === FALSE)
			{
				$this->error('删除失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('删除成功',url('admin/post/postlist'),null,0);
			}
			
		}
		
	}
	
	// 删除单条
	public function delposts(){
		
		if($this->request->isAjax()){
			
			// 获取参数
			$id = $this->request->param('id');
			
			$result = model('Post.Post')->find($id);
			
			if(!$result){
				$this->error('没有该帖子');
			}
				
			$result = model('Post.Post')->destroy(['id' => $id]);
			
			if($result){
				
				$this->success('删除成功',url('admin/post/postlist'),null,0);
				
			}else{
				
				$this->error('删除失败');
			}
		}
		
	}
	
	// 查看删除的帖子
	public function restorelist(){
		// whereNotNull('belong')  'deletetime', 'is not null'
		$restorelist = model('Post.Post')->onlyTrashed()->paginate(7,false,['query' => Request::instance()->param()]);
		
		return $this->fetch('',[
			
			'restorelist' => $restorelist
			
		]);
		
	}
	
	// 恢复帖子
	public function restore(){
		
		if($this->request->isAjax()){
			
			// 获取启动数据
			$arrstr = $this->request->param('str');
			
			// 字符串转数组
			$arr = explode(',',$arrstr);
			
			$data = [];
			
			// 循环
			foreach($arr as $item){
				
				$data[] = [
					'id' => $item,
					'deletetime' => null
				];
			}
			
			$result = model('Post.Post')->isUpdate(true)->saveAll($data);
			
			if($result === FALSE)
			{
				$this->error('恢复失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('恢复成功',url('admin/post/postlist'),null,0);
			}
			
		}
		
	}
	
	// 查询cate分类
	public function catelist(){
		// whereNotNull('belong')  'deletetime', 'is not null'
		$catelist = model('Post.Cate')->paginate(7,false,['query' => Request::instance()->param()]);
		
		return $this->fetch('',[
			
			'catelist' => $catelist
			
		]);
		
	}
	
	
	// 编辑帖子类型
	public function upcate(){ 
		
		// id
		$id = $this->request->param('id');
		
		$upcate = model('Post.Cate')->where('id',$id)->find();
		
		if($this->request->isPost()){
			
			//
			$params = $this->request->param();
			
			// 验证码验证
			if(!captcha_check($params['vercode'])){
				// 跳转中间页
				$this->error('验证码错误');
			}
			
			// 获取数据
			$id = $this->request->param('id');
			
			// 获取数据
			$name = $this->request->param('name');
			
			// 封装数据
			$data = [
				'id' => $id,
				'name' => $name
			];
			
			// 编辑
			// isUpdate == true ?  save = 更新数据  : 插入
			$result = model('Post.Cate')->isUpdate(true)->save($data);
			
			
			if($result === FALSE)
			{
				$this->error('修改失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('修改成功',url('admin/post/catelist'),null,0);
			}
			
		}
		
		// 动态使用模板布局
		// $this->view->engine->layout(false);
		return $this->fetch('',[
			
			'upcate' => $upcate
			
		]);
			
	}
	
	// 新增分类
	public function addcate(){
		
		if($this->request->isPost()){
			
			//
			$params = $this->request->param();
			
			// 验证码验证
			if(!captcha_check($params['vercode'])){
				// 跳转中间页
				$this->error('验证码错误');
			}
			
			// 获取数据
			$name = $this->request->param('name');
			
			// 封装数据
			$data = [
				'name' => $name
			];
			
			// 编辑
			// isUpdate == true ?  save = 更新数据  : 插入
			$result = model('Post.Cate')->save($data);
			
			
			if($result === FALSE)
			{
				$this->error('添加失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('添加成功',url('admin/post/catelist'),null,0);
			}
			
		}
		
		return $this->fetch();
		
	}
	
	// 删除分类
	public function delcate(){
		
		if($this->request->isAjax()){
			
			// 获取启动数据
			$arrstr = $this->request->param('str');
			
			// 字符串转数组
			$arr = explode(',',$arrstr);
			
			// 循环
			foreach($arr as $item){
				
				$result = model('Post.Cate')->destroy(['id' => $item]);
				
			}
			
			if($result === FALSE)
			{
				$this->error('删除失败');	
			}else{
				// cookie('LoginUser',null);
				$this->success('删除成功',url('admin/post/postlist'),null,0);
			}
			
		}
		
	}
	
}
